package com.wicket.okrapp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wicket.okrapp.base.service.MAssistBehaviorCurrentStepService;
import com.wicket.okrapp.base.service.dto.req.QueryContCurrentBstepDetailReq;
import com.wicket.okrapp.base.service.dto.req.QueryContCurrentBstepListReq;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.common.util.CommonFunctionHelper;
import com.wicket.okrapp.dal.mapper.mbg.AssistBehaviorCurrentStepMapper;
import com.wicket.okrapp.dal.po.mbg.AssistBehaviorCurrentStep;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MAssistBehaviorCurrentStepServiceImpl
        extends ServiceImpl<AssistBehaviorCurrentStepMapper, AssistBehaviorCurrentStep>
        implements MAssistBehaviorCurrentStepService {

    @Autowired
    AssistBehaviorCurrentStepMapper assistBehaviorCurrentStepMapper;

    /**
     * 1-1-18查询底层框架服务内容当前行为步骤
     */
    @Trace(operationName = "1-1-18查询底层框架服务内容当前行为步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AssistBehaviorCurrentStep queryContCurrentBstepDetail(QueryContCurrentBstepDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            List<AssistBehaviorCurrentStep> retList = (List<AssistBehaviorCurrentStep>) super.listByMap(columnMap);
            if(retList!=null && retList.size()>0){
                AssistBehaviorCurrentStep retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }

    /**
     * 1-1-18查询底层框架服务内容当前行为步骤列表
     */
    @Trace(operationName = "1-1-18查询底层框架服务内容当前行为步骤列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<AssistBehaviorCurrentStep> queryContCurrentBstepList(QueryContCurrentBstepListReq reqDto) {

        try {
            QueryWrapper<AssistBehaviorCurrentStep> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(AssistBehaviorCurrentStep.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(AssistBehaviorCurrentStep.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(AssistBehaviorCurrentStep.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<AssistBehaviorCurrentStep>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-18删除底层框架服务内容当前行为步骤
     */
    @Trace(operationName = "1-1-18删除底层框架服务内容当前行为步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteContCurrentBstep(String BehaviorCurrentStepId) {

        return super.removeById(BehaviorCurrentStepId);
    }

    /**
     * 1-1-18修改底层框架服务内容当前行为步骤
     */
    @Trace(operationName = "1-1-18修改底层框架服务内容当前行为步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateContCurrentBstep(AssistBehaviorCurrentStep assistBehaviorCurrentStep) {
        assistBehaviorCurrentStep.setInductionRecordId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        return super.updateById(assistBehaviorCurrentStep);
    }

    /**
     * 1-1-18新增底层框架服务内容当前行为步骤
     */
    @Trace(operationName = "1-1-18新增底层框架服务内容当前行为步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addContCurrentBstep(AssistBehaviorCurrentStep assistBehaviorCurrentStep) {
        assistBehaviorCurrentStep.setBehaviorCurrentStepId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        assistBehaviorCurrentStep.setInductionRecordId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        assistBehaviorCurrentStep.setIsValid("FALSE");
        assistBehaviorCurrentStep.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        assistBehaviorCurrentStep.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(assistBehaviorCurrentStep)) {
            return assistBehaviorCurrentStep.getBehaviorCurrentStepId();
        } else {
            return "-1";
        }

    }
}
